package com.jlkf.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jlkf.dao.TblUserMapper;
import com.jlkf.dao.TblUserPurseInfoMapper;
import com.jlkf.dao.TblUserWithdrawMapper;
import com.jlkf.pojo.TblUser;
import com.jlkf.pojo.TblUserPurseInfo;
import com.jlkf.pojo.TblUserWithdraw;
import com.jlkf.service.WithDrawService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.StringTools;
import com.platform.server.util.UUIDUtil;

@Service
public class WithDrawServiceImpl implements WithDrawService {
	@Autowired
    private TblUserWithdrawMapper tblUserWithdrawMapper;
	@Autowired
	private TblUserMapper tblUserMapper;
	@Autowired
	private TblUserPurseInfoMapper tblUserPurseInfoMapper;
	
	/**
	 * @param json
	 * @return
	 */
	@Override
	public ReturnData insertWithDraw(String json) {
		// TODO Auto-generated method stub
		try{
			SimpleDateFormat sf = new SimpleDateFormat("dd");
			Date date = new Date();
			/*if(sf.format(date).equals("25")){*/
				TblUserWithdraw withdraw = StringTools.json2Obj(json, TblUserWithdraw.class);
				/*if(tblUserWithdrawMapper.selectByUser(withdraw.getgUserid())>0){*/
					if(StringTools.isEmpty(withdraw.getgAccountBank())){
						return new ReturnData(500,"银行卡号不能为空");
					}
					if(StringTools.isEmpty(withdraw.getgBankName())){
						return new ReturnData(500,"开户银行不能为空");
					}
					if(!StringTools.checkBankCard(withdraw.getgAccountBank())) {
						return new ReturnData(500, "银行卡输入有误");
					}
					if(StringTools.isEmpty(withdraw.getgAccountName())){
						return new ReturnData(500,"账户姓名不能为空");
					}
					TblUser info = tblUserMapper.selectByPrimaryKey(Long.valueOf(withdraw.getgUserid()));
					if(info.getgUserMoney()<withdraw.getgMoney()){
						return new ReturnData(500,"该账户余额不足");
					}
					Map<String, Object> map = new HashMap<String,Object>();
					map.put("money", info.getgUserMoney()-withdraw.getgMoney());
					map.put("gId", info.getgId());
					int rel = tblUserMapper.updateBalance(map);
					if(rel>0){
						Calendar ca = Calendar.getInstance();
						ca.add(Calendar.DATE, 5);
						withdraw.setgCreateTime(new Date());
						withdraw.setgPredictDate(ca.getTime());
						withdraw.setgDealNo(UUIDUtil.generateShortUuid());
						int i = tblUserWithdrawMapper.insertSelective(withdraw);
						if(i>0){
							TblUserPurseInfo purseInfo = new TblUserPurseInfo();
							purseInfo.setgContent("提现处理中");
							purseInfo.setgCreatetime(new Date());
							purseInfo.setgMoney(withdraw.getgMoney());
							purseInfo.setgStatus(2);
							purseInfo.setgTitle("提现");
							purseInfo.setgType(1);
							purseInfo.setgBalance(info.getgUserMoney()-withdraw.getgMoney());
							purseInfo.setgUserid(Long.valueOf(withdraw.getgUserid()));
							purseInfo.setwId(withdraw.getgId());
							tblUserPurseInfoMapper.insertSelective(purseInfo);
							return new ReturnData(withdraw.getgId(),200,"提现发起成功，等待审核");
						}else{
							return new ReturnData(200,"提现出错");
						}
					}else{
						return new ReturnData(200,"提现出错");
					}
				/*}else{
					return new ReturnData(500,"每月只能提现一次");
				}
			}else{
				return new ReturnData(500,"每月25号提现");
			}*/
		}catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"系统错误");
		}
	}

	@Override
	public ReturnData getWithDrawDetail(Long id) {
		// TODO Auto-generated method stub
		TblUserWithdraw withdraw = tblUserWithdrawMapper.selectByPrimaryKey(id);
		return new ReturnData(withdraw,200,"提现详情");
	}

	@Override
	public ReturnData getWithDrawInfo(Long userId) {
		// TODO Auto-generated method stub
		TblUserWithdraw withdraw = tblUserWithdrawMapper.selectInfo(userId);
		return new ReturnData(withdraw,200,"最近提现记录");
	}

}
